#include "StdAfx.h"

#include "ColorUtils/Color.h"
#include "MathUtils/Ray.h"
#include "FirstIntersectionResult.h"
#include "SecondIntersectionResult.h"
#include "Intersectable.h"

#include "FlatSphere.h"

FlatSphere::FlatSphere(const Point& center, double radius, const Color& color) : _center(center)
	,_squaredRadius(radius * radius), _radius(radius), _color(color)
{}

void FlatSphere::WhereIntersects(const Ray& ray, FirstIntersectionResult& result)
{
	Point c(_center - ray.P);
	double temp = ray.D.ScalarProduct(c), temp2;
	double sum = temp * temp  - c.ScalarProduct(c) + _squaredRadius;
	if (sum > 0)
 		if ((temp2 = (temp - sqrt(sum))) > 0)
 			result.Set(temp2, this);
 		else if ((temp2 = (temp + sqrt(sum))) > 0)
 			result.Set(temp2, this);
		else
			result.Set(this);
	else
		result.Set(this);
}

void FlatSphere::ColorAtIntersection(SecondIntersectionResult& result, const Point&, const Ray&)
{
	result.ColorAtPoint = _color;
}

Direction FlatSphere::NormalAtPoint(FirstIntersectionResult&, const Point& point)
{
	return point - _center;
}

FlatSphere::~FlatSphere() {}
